<?php 
namespace app\Chat;
class Template{
	private $accesstoken; //公众号access token
	private $openid; //家长openid
	private $template;  //消息模板
	public function __construct($ast,$opd,$tpl) {
		$this->accesstoken = $ast;
		$this->openid = $opd;
		$this->template = $tpl;
	}
	public function send() {
		/*
		需要填写三项
		1.acccess_token
		2.touser : openid
		3.temlete_id：模板id  
		然后调用这个
		 */
		// $access_token = '7_qqq4DuUc_l7kjm3eNiqTWFJdgl6a6veA6mWFtiFxdIwZqSaFuSN5PuEZ8TmA1-AjK-OtQYZ05axjvcDfPtZDfNmWWZL81TxMUq6Lu1ymk8cfl7PxP-PpDqX3i7E1lyD__WL0y8JraFrk0NDnXQZaAIANGP';
		$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$this->accesstoken;//access_token改成你的有效值
		 
		$data = array(
		    'first' => array(
		        'value' => '有一名客户进行了一次预约！',
		        'color' => '#FF0000'
		    ),
		    'keyword1' => array(
		        'value' => '2015/10/5 14:00~14:45',
		        'color' => '#FF0000'
		    ),
		    'keyword2' => array(
		        'value' => '都会型SPA',
		        'color' => '#FF0000'
		    ),
		    'keyword3' => array(
		        'value' => '1cvvvv',
		        'color' => '#FF0000'
		    ),
		    'keyword4' => array(
		        'value' => '上海市浦东新区XXXXSPA馆',
		        'color' => '#FF0000'
		    ),
		    'remark' => array(
		        'value' => '请您务必准时到场为客户提供SPA服务！',
		        'color' => '#FF0000'
		    )
		);
		// $template_msg=array('touser'=>'oGgUHv5ieaNMwpnpj2yw7GzuUG-s','template_id'=>'ljtGg0XiPf5mTMjaRfLDGGmEx3fukTrDz90BQW8ug7Q','topcolor'=>'#FF0000','data'=>$data);
		$template_msg=array('touser'=>$this->openid,'template_id'=>$this->template,'topcolor'=>'#FF0000');

		 // oGgUHv5ieaNMwpnpj2yw7GzuUG-s
		$curl = curl_init($url);
		$header = array();
		$header[] = 'Content-Type: application/x-www-form-urlencoded';
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		// 不输出header头信息
		curl_setopt($curl, CURLOPT_HEADER, 0);
		// 伪装浏览器
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
		// 保存到字符串而不是输出
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		// post数据
		curl_setopt($curl, CURLOPT_POST, 1);
		// 请求数据
		curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
		$response = curl_exec($curl);
		curl_close($curl);
		echo $response;
	}
}

 ?>